import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}

/**
  * @author dinghao 
  * @create 2021-08-19 18:14 
  * @message
  */
object MongodbSource {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[*]")
      .appName("MongodbSource")
      .config("spark.mongodb.input.uri", ConnectMessage.get("spark.mongodb.input.uri"))
      .getOrCreate()
    val schema = StructType(
      List(
        StructField("name", StringType),
        StructField("age", IntegerType),
        StructField("sex", StringType)
      )
    )
    // 通过schema约束，直接获取需要的字段
    val df = spark.read.format("com.mongodb.spark.sql").schema(schema).load()
    df.show()
    df.createOrReplaceTempView("user")
    val resDf = spark.sql("select * from user")
    resDf.show()
    spark.stop()
  }
}
